package  
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import iso.*;
	
	public final class BoxTest extends Sprite 
	{
		
		public function BoxTest() 
		{
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			m_tiles = new Sprite();
			m_tiles.x = this.stage.stageWidth / 2;
			m_tiles.y = 100;
			this.addChild(m_tiles);
			
			m_boxes = new Sprite();
			m_boxes.x = this.stage.stageWidth / 2;
			m_boxes.y = 100;
			this.addChild(m_boxes);
			
			for (var i : int = 0; i < 20; ++i)
			{
				for (var j : int = 0; j < 20; ++j)
				{
					var tile : DrawnIsoTile = new DrawnIsoTile(20, 0xcccccc);
					tile.position = new Point3D(i * 20, -0.1, j * 20);
					m_tiles.addChild(tile);
				}
			}
			
			this.addEventListener(MouseEvent.CLICK, onMouseClickHandler);
			
			m_isoObjects = new Vector.<IsoObject>();
		}
		
		private function onMouseClickHandler(evt : MouseEvent) : void
		{
			var box : DrawnIsoBox = new DrawnIsoBox(20, Math.random() * 0xffffff, 20);
			var pos : Point3D = IsoUtils.screenToIso(new Point(m_tiles.mouseX, m_tiles.mouseY));
			pos.x = Math.round(pos.x / 20) * 20;
			pos.y = Math.round(pos.y / 20) * 20;
			pos.z = Math.round(pos.z / 20) * 20;
			box.position = pos;
			m_isoObjects.push(box);
			m_isoObjects.sort(sortObjects);
			m_boxes.addChild(box);
			
			for (var i : int = 0; i < m_isoObjects.length; ++i)
				m_boxes.setChildIndex(m_isoObjects[i], i);
		}
		
		private function sortObjects(obj1 : IsoObject, obj2 : IsoObject) : Number
		{
			return obj1.depth - obj2.depth;
		}
		
		private var m_tiles : Sprite = null;
		private var m_boxes : Sprite = null;
		private var m_isoObjects : Vector.<IsoObject> = null;
	}

}